package net.itgoo.parkingpay.ui.payconfirm;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import net.itgoo.parkingpay.rest.ParkingVolleyManager;
import net.itgoo.parkingpay.rest.model.ParkingOrderModel;
import net.itgoo.parkingpay.rest.model.ParkingPrePayModel;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPLoadDataCallback;

import org.json.JSONException;
import org.json.JSONObject;

public class ParkingPayConfirmDataRepository implements ParkingPayConfirmContract.DataSource {

    private ParkingOrderModel mParkingOrderModel;

    @Override
    public void createOrder(String userId, long id, final ParkingMVPLoadDataCallback callback) {
        final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("appuserid", userId);
            jsonObject.put("riid", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_createorder.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        if (response.has("ordernum")) {
                            mParkingOrderModel = ParkingOrderModel.parse(response);
                            callback.onDataLoaded(mParkingOrderModel);
                        } else {
                            callback.onDataError("");
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public void prePay(String orderNo, String sessionId, final ParkingMVPLoadDataCallback callback) {
        final JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderno", orderNo);
            jsonObject.put("sessionId", sessionId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "getPayUrl.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        int code = 0;
                        ParkingPrePayModel prePay = new ParkingPrePayModel();
                        try {
                            code = response.getInt("code");
                            prePay.url = response.getString("url");
                            prePay.transactionNo = response.getString("transactionno");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (code == 0) {
                            callback.onDataLoaded(prePay);
                        } else {
                            callback.onDataError("");
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public ParkingOrderModel order() {
        return mParkingOrderModel;
    }

    @Override
    public void cancel() {

    }
}
